package whf.framework.lucene.searcher;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Searcher;

import whf.framework.lucene.IndexConfig;

/**
 * @author king
 * @modify 2008-4-13 下午11:04:45
 * 
 */
public class SearcherManager {
	public static IndexReader [] INDEXREADERS = null;
	
	public static void closeIndexReaders() throws IOException {		
		if(INDEXREADERS == null || INDEXREADERS.length ==0 )return;
		for( int i = 0; i < INDEXREADERS.length ;  i++){
			if(INDEXREADERS[i] != null)INDEXREADERS[i].close();			
		}
		INDEXREADERS = null;
	}
	
	public static Searcher getSearcher() throws IOException{
		if(INDEXREADERS == null || INDEXREADERS.length ==0 ){
			File file = new File(IndexConfig.getIndexDirectory());
			File dirs [] = file.listFiles();
			INDEXREADERS = new IndexReader[dirs.length];
			for( int i = 0; i < dirs.length ;  i++){
				INDEXREADERS[i] = IndexReader.open(dirs[i].getPath().toString());
			}
		}
		IndexSearcher indexSearcher[] = new IndexSearcher[INDEXREADERS.length];
		for( int i = 0; i < INDEXREADERS.length ;  i++){
			indexSearcher[i] = new IndexSearcher(INDEXREADERS[i]);
		}
		return new MultiSearcher(indexSearcher);
	}
}
